package cn.monkey.io.transport.netty.server.codec.protocol.tcp.http.ws;

import cn.monkey.io.transport.netty.server.codec.protocol.AbstractServerProtocolRouterFunction;
import cn.monkey.io.transport.netty.server.codec.protocol.tcp.http.HttpServerProtocolConfig;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig;

import java.util.function.BiPredicate;
import java.util.function.Consumer;

public class WebsocketServerProtocolRouterFunction extends AbstractServerProtocolRouterFunction {

    public WebsocketServerProtocolRouterFunction(HttpServerProtocolConfig httpServerProtocolConfig,
                                                 WebSocketServerProtocolConfig webSocketServerProtocolConfig,
                                                 BiPredicate<Channel, Object> predicate,
                                                 Consumer<Channel> customizer) {
        super(WebsocketServerProtocolPredicate.INSTANCE.and(predicate), (new WebsocketServerProtocolConsumer() {
            @Override
            public WebSocketServerProtocolConfig webSocketServerProtocolConfig() {
                return webSocketServerProtocolConfig;
            }

            @Override
            public HttpServerProtocolConfig httpServerProtocolConfig() {
                return httpServerProtocolConfig;
            }
        }).andThen(customizer));
    }

    public WebsocketServerProtocolRouterFunction(WebSocketServerProtocolConfig serveConfig,
                                                 BiPredicate<Channel, Object> predicate,
                                                 Consumer<Channel> consumer) {
        this(HttpServerProtocolConfig.defaultConfig(), serveConfig, predicate, consumer);
    }

    public WebsocketServerProtocolRouterFunction(WebSocketServerProtocolConfig serveConfig,
                                                 Consumer<Channel> consumer) {
        super(WebsocketServerProtocolPredicate.INSTANCE, (new WebsocketServerProtocolConsumer() {
            @Override
            public WebSocketServerProtocolConfig webSocketServerProtocolConfig() {
                return serveConfig;
            }

            @Override
            public HttpServerProtocolConfig httpServerProtocolConfig() {
                return HttpServerProtocolConfig.defaultConfig();
            }
        }).andThen(consumer));
    }
}
